package cn.wsalix.msg.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.admin.BaseUser;
import cn.anlaser.page.QueryPage;
import cn.anlaser.service.impl.CommonServiceImpl;
import cn.anlaser.utils.BeanMapper;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.msg.entity.Chat;
import cn.wsalix.msg.entity.Issue;
import cn.wsalix.msg.repository.ChatRepository;
import cn.wsalix.msg.service.ChatService;
import cn.wsalix.msg.service.IssueService;
import cn.wsalix.user.form.ChatForm;
import cn.wsalix.utils.UserUtils;

@Service("chatService")
public class ChatServiceImpl extends
		CommonServiceImpl<ChatForm, ChatRepository, Chat> implements
		ChatService {
	@Autowired
	private ChatRepository chatRepository;
	@Autowired
	private IssueService issueService;
	@Autowired
	private UserUtils userUtils;

	@Override
	public String module() {
		return "/user/chat";
	}

	@Override
	public ChatRepository getRepository() {
		return chatRepository;
	}

	@Override
	public Class<Chat> getEntityClazz() {
		return Chat.class;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(ChatForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getIssueId() != null && form.getIssueId() != 0) {
			filters.put("issue.id", new SearchFilter("issue.id", Operator.EQ,
					form.getIssueId()));
		}
		if (form.isForward()) {
			filters.put("sendTime", new SearchFilter("sendTime", Operator.GTE,
					form.getSendTime()));
		} else {
			filters.put("sendTime", new SearchFilter("sendTime", Operator.LTE,
					form.getSendTime()));
		}
		if (form.getSenderId() != null && form.getSenderId() != 0) {
			filters.put("senderId", new SearchFilter("senderId", Operator.EQ,
					form.getSenderId()));
		}
		if (form.getReceiveId() != null && form.getReceiveId() != 0) {
			filters.put("receiveId", new SearchFilter("receiveId", Operator.EQ,
					form.getReceiveId()));
		}
		return filters;
	}

	@Transactional
	@Override
	public Chat sendWaiterChat(ChatForm form) {
		Issue issue = issueService.findByProductId(form.getProductId(),form.getUserId());
		Chat chat = BeanMapper.map(form, Chat.class);
		chat.setIssue(issue);
		chat.setSenderId(issue.getPresbyterId());
		chat.setSenderName(issue.getPresbyter());
		chat.setReceiveId(issue.getWaiterId());
		chat.setReceiveName(issue.getWaiterName());
		chat.setSendTime(new Date());
		return add(chat);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.wsalix.user.service.ChatService#sendClientChat(cn.wsalix.user.form
	 * .ChatForm) issueId context
	 */
	@Override
	public Chat sendClientChat(ChatForm form) {
		BaseUser operate = userUtils.getInUser();
		Issue issue = issueService.findById(form.getId());
		Chat chat = BeanMapper.map(form, Chat.class);
		chat.setIssue(issue);
		chat.setSenderId(operate.getId());
		chat.setSenderName(operate.getUsername());
		chat.setReceiveId(issue.getPresbyterId());
		chat.setReceiveName(issue.getPresbyter());
		chat.setSendTime(new Date());
		return super.add(chat);
	}

	@Override
	public List<Chat> findsByProduct(ChatForm form, Pageable pageable) {
		Issue issue = issueService.findByProductId(form.getProductId(),form.getUserId());
		form.setIssueId(issue.getId());
		QueryPage<Chat> resPage=findAll(form,pageable);
		return resPage.getPage().getContent();
	}

}
